import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_audioplayers.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_title_content_view.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/utils/file_util.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/get_task_record_by_unit_id_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/danger_investigation_page/investigation_plan_ledger/investigation_plan_ledger_detail/investigation_plan_ledger_detail/igation_ledger_detail_controller.dart';
import 'package:url_launcher/url_launcher.dart';

///排查计划详情详情
class InvestigationPlanLedgerDetailDetailPage extends StatelessWidget {
  InvestigationPlanLedgerDetailDetailPage({super.key});

  final controller = Get.put(InvestigationPlanLedgerDetailDetailController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            appBar: CustomAppBar(
              title: Text(
                '排查计划详情',
                style: TextStyle(
                  fontSize: 18.sp,
                  color: Colors.white,
                  fontWeight: FontWeight.w400,
                ),
              ),
              leading: const BackButton(
                color: Colors.white,
              ),
              iconButtonColor: Colors.white,
              backgroundColor: const Color(0xFF5C6DFF),
            ),
            body: Container(
                color: const Color(0xFFF5F5F6),
                child: Stack(
                  children: [
                    _getBasicInformation(),
                    Container(
                      margin: EdgeInsets.only(top: 54.w),
                      padding: EdgeInsets.symmetric(horizontal: 16.w),
                      child: ListView.separated(
                          itemBuilder: (context, index) {
                            return _getCapacityView(controller.entityList[index]);
                          },
                          separatorBuilder: (context, index) => AppGaps.vGap16,
                          itemCount: controller.entityList.length),
                    )
                  ],
                )),
          );
        });
  }

  /// 获取item详情视图
  Widget _getDetailsView(GetTaskRecordByUnitIdEntity entity) {
    return Container(
      alignment: Alignment.center,
      padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 14.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(
          Radius.circular(6.w),
        ),
      ),
      child: Column(
        children: [
          Align(
            alignment: Alignment.centerLeft,
            child: Text(
              '巡检描述：${entity.inspectionDescription ?? ''}',
              style: TextStyle(
                fontSize: 13.sp,
                fontWeight: FontWeight.w400,
                color: const Color(0xFFA5A6B5),
              ),
            ),
          ),
          AppGaps.vGap8,
          Align(
            alignment: Alignment.centerLeft,
            child: Text(
              '拍照:',
              style: TextStyle(
                fontSize: 13.sp,
                fontWeight: FontWeight.w400,
                color: const Color(0xFFA5A6B5),
              ),
            ),
          ),
          AppGaps.vGap8,
          Align(
            alignment: Alignment.centerLeft,
            child: Wrap(
                spacing: 5.w,
                children: entity.inspectionPhotos!.map<Widget>((item) {
                  return Container(
                    width: 74.w,
                    height: 74.w,
                    margin: EdgeInsets.only(bottom: 10.w),
                    decoration: BoxDecoration(
                      color: const Color(0xffF7F7FB),
                      border: Border.all(
                        color: const Color(0xffEBEDF0),
                      ),
                      borderRadius: BorderRadius.circular(8.w),
                      image: DecorationImage(
                        image: NetworkImage(Interface.fileUrl + item),
                        fit: BoxFit.cover,
                      ),
                    ),
                  ).clickShowLarge(url: Interface.fileUrl + item);
                }).toList()),
          ),
          AppGaps.vGap8,
          Align(
            alignment: Alignment.centerLeft,
            child: Text(
              '视频:',
              style: TextStyle(
                fontSize: 13.sp,
                fontWeight: FontWeight.w400,
                color: const Color(0xFFA5A6B5),
              ),
            ),
          ),
          AppGaps.vGap8,
          Align(
            alignment: Alignment.centerLeft,
            child: Wrap(
                spacing: 5.w,
                children: entity.inspectionVideos!.map<Widget>((item) {
                  return Container(
                    width: 160.w,
                    height: 90.w,
                    margin: const EdgeInsets.only(bottom: 10),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      color: const Color(0xff060B15),
                      borderRadius: BorderRadius.circular(8),
                    ),
                    child: IconButton(
                      icon: const Icon(
                        Icons.play_circle,
                        color: Colors.white,
                        size: 30,
                      ),
                      onPressed: () {
                        FileUtils.openFile(Interface.fileUrl + item);
                      },
                    ),
                  );
                }).toList()),
          ),
          AppGaps.vGap8,
          Align(
            alignment: Alignment.centerLeft,
            child: Text(
              '语音:',
              style: TextStyle(
                fontSize: 13.sp,
                fontWeight: FontWeight.w400,
                color: const Color(0xFFA5A6B5),
              ),
            ),
          ),
          AppGaps.vGap8,
          entity.patrolVoices!.isNotEmpty
              ? CustomAudioplayers(
                  audioUrl: entity.patrolVoices!,
                  isEnabled: false,
                )
              : Container(),
        ],
      ),
    );
  }

  Widget _getBasicInformation() {
    return Container(
        height: 84.w,
        width: AppDimens.sh,
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.w),
        color: const Color(0xFF5C6DFF),
        child: Column(
          children: [
            Row(
              children: [
                SvgPicture.asset(
                  AppImages.icon_ledger_icon,
                  width: 17.w,
                  height: 17.w,
                ),
                AppGaps.hGap6,
                Expanded(
                    child: Text(
                  controller.riskUnitName,
                  style: TextStyle(
                    fontSize: 16.sp,
                    color: Colors.white,
                    overflow: TextOverflow.ellipsis,
                    fontWeight: FontWeight.w500,
                  ),
                )),
              ],
            ),
          ],
        ));
  }

  Widget _getCapacityView(GetTaskRecordByUnitIdEntity entity) {
    return CustomTitleContentView(
        title: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SvgPicture.asset(
              AppImages.icon_title,
              width: 20.w,
              height: 20.w,
            ),
            AppGaps.hGap10,
            Expanded(
              child: Text(
                entity.riskMeasureDesc ?? '',
                style: TextStyle(
                  fontSize: 15.sp,
                  color: const Color(0xFF333333),
                  fontWeight: FontWeight.w500,
                ),
              ),
            ),
            _getStateView(entity.checkStatus ?? 0),
            AppGaps.hGap10,
          ],
        ),
        content: _getDetailsView(entity));
  }

  /// 获取状态按钮
  Widget _getStateView(int checkStatus) {
    Color? color;
    String? status;
    switch (checkStatus) {
      case 0:
        status = '正常';
        color = const Color(0xFF00CE78);
        break;
      case 1:
        status = '存在隐患';
        color = const Color(0xFFFFCE24);
        break;
      case 2:
        status = '漏检';
        color = const Color(0xFFFD6B6B);
        break;
    }
    return Container(
      width: 52.w,
      height: 18.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(
          color: color,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(10.r),
            topRight: Radius.circular(10.r),
            bottomRight: Radius.circular(10.r),
          )),
      child: Text(
        status ?? '',
        style: TextStyle(
          color: Colors.white,
          fontSize: 10.sp,
          fontWeight: FontWeight.w400,
        ),
      ),
    );
  }
}
